﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlanetViewer
{
    public class DetailGroup
    {
        public int Length;
        public float[] Data;
        public List<int> DetailPoints;
        public float _Height;
        public bool _Land;

        public DetailGroup(int length, Random rand, int dir = 1, float height = 1.0f, float deadzone = 0.3f)
        {
            this.Length = length;
            this._Height = height;
            this.Data = new float[length];
            this.DetailPoints = new List<int>();

            _Land = (bool)(dir > 1);

            int dPMinDelta = (int)Math.Ceiling((double)length / 8); //Distance between detail points

            int dPDelta = dPMinDelta + rand.Next(dPMinDelta); //Randomise this distance
            int idPDelta = dPDelta; //Save the initial value

            for (int t = idPDelta; t < length - dPMinDelta; t += dPDelta)
            {
                float newDetail = height * dir * (deadzone + (1 - deadzone) * (float)rand.NextDouble()); //Create detail points in the current anchor group
                DetailPoints.Add(t);
                Data[t] = newDetail;

                int interpPos = t - dPDelta; //Position of the interpolation group

                if (t == idPDelta)
                {
                    float[] interp = util.HalfSineInterp(Data[interpPos], newDetail, dPDelta);
                    interp.CopyTo(Data, interpPos + 1);
                }
                else
                {
                    float[] interp = util.CosineInterp(Data[interpPos], newDetail, dPDelta);
                    interp.CopyTo(Data, interpPos + 1);
                }

                dPDelta = dPMinDelta + rand.Next(dPMinDelta);

                if (t + dPDelta >= length - dPMinDelta)
                {
                    int delta = length - t;

                    float[] interp = util.HalfCosineInterp(Data[t], Data[0], delta);
                    interp.CopyTo(Data, t + 1);
                }
            }
        }
    }
}
